﻿using System.Collections.Generic;
using System.Data.SqlClient;

namespace BLL.Services.WikiCategories
{
    public class WikiCategoryService : IWikiCategoryService
    {
        private readonly WikiCategoryCollection _wikiCategoryCollection;

        public WikiCategoryService(WikiCategoryCollection wikiCategoryCollection)
        {
            _wikiCategoryCollection = wikiCategoryCollection;
        }

        public IEnumerable<WikiCategory> SelectAll()
        {
            try
            {
                return _wikiCategoryCollection.SelectAll();
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public WikiCategory GetById(int wikiCategoryId)
        {
           try
           {
               return _wikiCategoryCollection.GetById(wikiCategoryId);
           }
           catch (SqlException sqlException)
           {
               throw new ServiceException(sqlException.Message);
           }
        }

        public void Create(WikiCategory model)
        {
            try
            {
                _wikiCategoryCollection.Create(model);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public void Save(WikiCategory model)
        {
            try
            {
                _wikiCategoryCollection.Save(model);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public void Delete(int wikiCategoryId)
        {
            try
            {
                _wikiCategoryCollection.Delete(wikiCategoryId);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public bool ValidateWikiCategory(int wikiCategoryId, string name)
        {
            try
            {
                return _wikiCategoryCollection.ValidateWikiCategory(wikiCategoryId, name);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        public Listing.WikiCategoryListing GetWikiCategoryListing(int page, int size, string column, int direction, Listing.WikiCategoryFilter filter)
        {
            try
            {
                return _wikiCategoryCollection.GetWikiPagesListing(page, size, column, direction, filter);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }
    }
}
